package com.yisoo.ubag.iot.service.impl;

import com.aliyuncs.iot.model.v20180120.CreateProductResponse;
import com.aliyuncs.iot.model.v20180120.CreateProductTopicResponse;
import com.aliyuncs.iot.model.v20180120.CreateSubscribeRelationResponse;
import com.yisoo.common.to.IotProductDataTo;
import com.yisoo.common.utils.R;
import com.yisoo.ubag.iot.aliyun.sdk.openapi.ProductManager;
import com.yisoo.ubag.iot.aliyun.sdk.openapi.TopicManager;
import com.yisoo.ubag.iot.entity.TopicEntity;
import com.yisoo.ubag.iot.service.TopicService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Map;
import java.util.UUID;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yisoo.common.utils.PageUtils;
import com.yisoo.common.utils.Query;

import com.yisoo.ubag.iot.dao.ProdDao;
import com.yisoo.ubag.iot.entity.ProdEntity;
import com.yisoo.ubag.iot.service.ProdService;


@Service("prodService")
public class ProdServiceImpl extends ServiceImpl<ProdDao, ProdEntity> implements ProdService {


    @Autowired
    TopicService topicService;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ProdEntity> page = this.page(
                new Query<ProdEntity>().getPage(params),
                new QueryWrapper<ProdEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public R addproduct(Long cid, String pname) {
//        createProductTest(productName,0,"iothub_senior",0,null,null,"customize","CELLULAR","secret")

        CreateProductResponse.Data data = ProductManager.createProductTest(pname, 0,
                "iothub_senior", 0, null,
                null, "customize", "CELLULAR", "secret");
        IotProductDataTo productDataTo = new IotProductDataTo();
        BeanUtils.copyProperties(data,productDataTo);
        productDataTo.setProductKey(data.getProductKey());
        try {
            Thread.sleep(300);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        if(productDataTo.getProductSecret() == null){
            return R.error("产品创建失败");
        }
        ProdEntity prodEntity = new ProdEntity();
        prodEntity.setConsumerId(cid);
        prodEntity.setProductKey(productDataTo.getProductKey());
        prodEntity.setProductName(productDataTo.getProductName());
        prodEntity.setProductSecret(productDataTo.getProductSecret());
        this.save(prodEntity);
//        System.out.println(data.getProductKey());
//        System.out.println(data.getProductSecret());
//        System.out.println(data.getProductName());


        //创建服务器订阅
        this.addproductSubAndPub(prodEntity.getProductKey());
        //创建必要的topic
        this.addTopicAuto(prodEntity.getProductKey());
        return R.ok().put("data",productDataTo);
    }

    @Override
    public R addproductSubAndPub(String productKey) {

        CreateSubscribeRelationResponse res = ProductManager.CreateSubscribeRelation(productKey);

        if (res.getSuccess() ){
            return R.ok();
        }
        return R.error();
    }

    @Override
    public R addTopicAuto(String productKey) {
        //添加自定义的topic 这个应该在创建product完成之后被调用
//        String common = "user/";
        String SubOrder = "sub/order";//服务器命令帧
        String SubResp = "sub/resp";//服务器回复帧
        String PubStatus = "pub/status"; //设备汇报帧
        String PubAck = "pub/ack"; //设备确认帧

        Long topicId1 = TopicManager.createProductTopic(
                productKey,
                SubOrder,
                "SUB",
                "服务器命令帧"
        ).getTopicId();

        topicService.save(new TopicEntity(null,productKey,SubOrder,"SUB","服务器命令帧",String.valueOf(topicId1)));
        Long topicId2 = TopicManager.createProductTopic(
                productKey,
                SubResp,
                "SUB",
                "服务器回复帧"
        ).getTopicId();
        topicService.save(new TopicEntity(null,productKey,SubResp,"SUB","服务器回复帧",String.valueOf(topicId2)));
        Long topicId3 = TopicManager.createProductTopic(
                productKey,
                PubStatus,
                "PUB",
                "设备汇报帧"
        ).getTopicId();
        topicService.save(new TopicEntity(null,productKey,PubStatus,"PUB","设备汇报帧",String.valueOf(topicId3)));

        Long topicId4 = TopicManager.createProductTopic(
                productKey,
                PubAck,
                "PUB",
                "设备确认帧"
        ).getTopicId();
        topicService.save(new TopicEntity(null,productKey,PubAck,"PUB","设备确认帧",String.valueOf(topicId4)));


        if (topicId1!=null&&topicId2!=null&&topicId3!=null&&topicId4!=null){
            return R.ok();
        }else {
            return R.error();
        }
    }

}